import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class FileChooser extends JFrame {
	private static final long serialVersionUID = 1L;

	ActionListener actionListener = new ActionListener() {
		public void actionPerformed(ActionEvent actionEvent) {
			JFileChooser theFileChooser = (JFileChooser) actionEvent.getSource();
			String command = actionEvent.getActionCommand();
			if (command.equals(JFileChooser.APPROVE_SELECTION)) {
				
				File selectedFile = theFileChooser.getSelectedFile();
				
				try {
					String filePath = selectedFile.getCanonicalPath();
					String shortFileName = selectedFile.getName();
					FileOnlyClient fileOnlyClient = new FileOnlyClient();
					fileOnlyClient.run("127.0.0.1", 5001, filePath, shortFileName);
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					
				}
				
			/*	
				System.out.println(selectedFile.getParent());
				System.out.println(selectedFile.getName());
				*/
				
			} else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
				System.out.println(JFileChooser.CANCEL_SELECTION);
				
			}
		}
	};
	

	public FileChooser() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setDialogTitle("Choose a file");
		this.getContentPane().add(fileChooser);
		fileChooser.setVisible(true);
		fileChooser.addActionListener(actionListener);
	}

	public void run() {
		JFrame frame = new FileChooser();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.pack();
		frame.setVisible(true);
	}


}
